package cn.sofwin.web.blog.servlet;

import cn.sofwin.web.blog.entity.Admin;
import cn.sofwin.web.blog.service.AdminService;
import cn.sofwin.web.blog.service.ArticleService;
import cn.sofwin.web.blog.service.impl.AdminServiceImpl;
import cn.sofwin.web.blog.service.impl.ArticleServiceImpl;
import cn.sofwin.web.blog.utils.HttpHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 管理员Servlet
 * @author ZX
 * @version 1.0
 */
@WebServlet("/blog/admin")
public class AdminServlet extends HttpServlet {

    /** 用户服务接口 */
    private AdminService adminService = AdminServiceImpl.getInstance();

    private ArticleService articleService= ArticleServiceImpl.getInstance();

    @Override
    protected void doGet(HttpServletRequest req , HttpServletResponse resp) throws ServletException, IOException {

        HttpHelper.charset(req);
        String name = req.getParameter("name");
        Admin admin=adminService.findByName(name);
        HttpHelper.charset(resp);
        req.getSession().setAttribute("ADMIN",admin);


        List list=articleService.all();

        req.setAttribute("list",list);

        req.getRequestDispatcher("/manager.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
